﻿Imports System.Windows.Forms

Public Class Acad_ImportColumnsDialog

    Dim ColumnList As New List(Of String)

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        'lets validate first
        Dim c As Integer

        Dim ignore As Integer = 0
        ColumnList.Clear()

        'check to see if anything has been selected and also if there are any duplicates
        For c = 0 To ColumnsGrid.Rows.Count - 1
            If ColumnsGrid.Item(1, c).Value = "0" Then
                ignore = ignore + 1
            End If

            If FindCode(ColumnsGrid.Item(1, c).Value) Then
                MessageBox.Show("There are some duplicate fields selected.  Please make sure that all Excel columns are linked to a unique field in The Store", "Duplicate fields found", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
        Next

        If ignore = ColumnsGrid.Rows.Count Then
            MessageBox.Show("You selected all fields to be ignored, hence nothing will be imported", "Nothing to import", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Function FindCode(ByVal FieldCode As String) As Boolean

        If FieldCode = "0" Then
            Return False
            Exit Function
        End If

        If ColumnList.Count = 0 Then
            ColumnList.Add(FieldCode)
            Return False
            Exit Function
        End If

        Dim c As Integer

        For c = 0 To ColumnList.Count - 1
            If ColumnList(c) = FieldCode Then
                Return True
                Exit Function
            End If
        Next

        ColumnList.Add(FieldCode)
        Return False
        Exit Function

    End Function

End Class
